package org.nobody.mallb2c.mallorder.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.nobody.mallb2c.mallorder.vo.PayVo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

  //在支付宝创建的应用的id
  private String app_id = "2016102500756700";

  // 商户私钥,您的PKCS8格式RSA2私钥
  private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjqlFJSU9knUwTfDD54Ft+NJTZyboxmsqk+c2K6vQL/WDjnpPZZCnEPBpgBUcWwqRH08+E4SHPR1CBTy3wODZM0DmgCbWDiKeIO+34Il6SoM4y6X0K+3fRyYSk03C0d0q5P7ghltPKe+GI2fHXzOTi3uzbi2yyx2GBjaHO/xsLEbv3G/VV+g7O5y+kc+J5UpJmtlKEnjawkq9RoICUoeI6J4YofGmlwr16kfDQFa4GwU6bIZ1x3KOzU8fu2pBWBM2PkcIcr0vaz3v82hlGFo7mpYre2tb3faawXQRsgq6eNwGWWrXJmxP2EXPKog1YoS8vpgkz9BrPoNfNUtBu9vTbAgMBAAECggEAK+reGR5U8ZjL+0aOFd9AbeNKNg7NqdXxnsdxgl990WHSiAHbhrA8DO5b3XxNESx1zNhnjS6kVey7C5flqdpLp90QOYOhc4ROAF6kgrsmSY5hBoRlC3djNaYvs9MRITwJjyAsFsdUWbdYU9K39MKEgOR1BoypRGgAIDWTP+pwpU1Uuk9+h4bD3etqGCSjuqUCfysjwNOfAfLfgQbgQ6FFvzBR+8wPAkJUUGrbYGHOp56JIwmXFHER+Dgb5/E+Dn2v4TWRlbTVH3hH0qc7UohujhNuGVcpPictz1bS3nyN8nJb2FN1AOZFVG37K+QBw5M+dYrB0tPro7tf5M+m0DO+4QKBgQDeU4IRdZHh6tvSX4LpEd7xjFOivuYRirVjtvK/5Y2rCWB8esutLFVan3IWPfslB/2z6krycqF+5BB0g6oEp7kgC2i2NsQ8Uk7WF8ZzYVlfIGrsnEKICavqpNOzzL6ouB8nFHuiLy/1rq1Vsxe8PZhfkRHNGYCLcmlngLuHq1KMswKBgQC8dEmcRS5Vo/mL27Zelf6LRuYfiq9iWhjIiL0qEchLkEy93R3YEeDQqdE4PeN3IKKxwV1ghdK8GL7pysiUTW/VAtCHNO1+gvrDQeCGKaBggP71/6wn1rnGMsXegclfrAkHvZgxjm8ag1Fft4i4d3T4IoGcazA90hhDULPhNSZbOQKBgC6KPqUW/nXTazbLpaTSh9/6pxtfaNvtNHFuP7yp+hD+woLsVaynpip1TDWZvfQPiy1Zc1zzYBkm9sbdNP8KeTdbDA1ww315IWWxzmoiy8mdCKUFROwjUiipXmO6DYNWH7dXk1XtGNYOPdy9w880+auvu5E6sG0Ag04WZatN0tb/AoGAei8/RqWIDBibhDU4wYyok66tJ0govDDEHsihNPSAk5WEXBlP7zZwN3nS348ouqUbNvqyyxEY98FnjndeouCuIEKJ5w/G556ObSh6JSI6DDe2ltJk0ROkMT/cEI6/fFszI6SmV4MMv/jiOpfpeghhezwMItlQVtSvzH03k019h+kCgYEAzdaPowYnnxrnXVdlKGFfaoQliZOmCFb30ASOsXtdnUpigF9x4Yx1+GD4oT+J5VIXz/Vp5V88BI7kwiFG/1P7bnXJUVt/N27iFLTSKb2izCpXIzgVuv487OYyfi+qlBi0A0rGayZ5HnOHKjIjuVH+dQtdPimUWPw6exch5yJHaHU=";
  // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm6/zWS+AjeNMwHsuP0tg15WBZPVT+9nXI92O94Lju11F1B5VMzn2gFopeCSXwcV3yNMPrqp1TDQXA7n883QHxbgtrHO7GjXwOmyjxennYQ1bsKGhNVmUyv6rt03jNa55if8L2pUVb5GPO/zU9xDi9FpK0NSHYh7zFTcrGv+kPiT2XWrlxhSWBSNxURdw4hmx+5o9UkmmaqCr1OL5TrsYoYiQYrNoLE0P3huCYk+MvVVRIXz5aUz9xNzEdu9usTnFYNk6YTr1GrKcDSwmqFhupaUAm4ZNQ3X+ysS5edFxBlicp+FTlyuKyOFfjj08jvxvv8F8XTxzN0DvoCAJiYmr6wIDAQAB";
  // 服务器[异步通知]页面路径
  // 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
  // 支付宝会悄悄的给我们发送一个请求,告诉我们支付成功的信息
  private String notify_url = "http://3fqkt3.natappfree.cc/payed/notify";
  // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
  // 同步通知,支付成功,一般跳转到成功页
  private String return_url = "http://member.mall.com/memberOrder.html";
  // 签名方式
  private String sign_type = "RSA2";
  // 字符编码格式
  private String charset = "utf-8";
  // 支付宝网关: https://openapi.alipaydev.com/gateway.do
  // 如果应用上线 应该更改为线上地址
  private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
  // 支付超时时间
  private String timeout = "1m";

  public String pay(PayVo vo) throws AlipayApiException {

    //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
    //1、根据支付宝的配置生成一个支付客户端
    AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
      app_id, merchant_private_key, "json",
      charset, alipay_public_key, sign_type);

    //2、创建一个支付请求 //设置请求参数
    AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
    alipayRequest.setReturnUrl(return_url);
    alipayRequest.setNotifyUrl(notify_url);

    //商户订单号,商户网站订单系统中唯一订单号,必填
    String out_trade_no = vo.getOut_trade_no();
    //付款金额,必填
    String total_amount = vo.getTotal_amount();
    //订单名称,必填
    String subject = vo.getSubject();
    //商品描述,可空
    String body = vo.getBody();

    //更多请求参数,参看: https://opendocs.alipay.com/apis/api_1/alipay.trade.page.pay/
    alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
      + "\"total_amount\":\"" + total_amount + "\","
      + "\"subject\":\"" + subject + "\","
      + "\"body\":\"" + body + "\","
      + "\"timeout_express\":\"" + timeout + "\"," //←← 支付多少分钟后超时
      + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

    String result = alipayClient.pageExecute(alipayRequest).getBody();

    //会收到支付宝的响应,响应的是一个页面,只要浏览器显示这个页面,就会自动来到支付宝的收银台页面
    System.out.println("支付宝的响应：" + result);

    return result;
  }
}
